package org.yx.service.sbas.save_warehouse_definition;

import org.yx.exception.BizException;
import org.yx.models.SbasStoDef;
import org.yx.utils.BaseService;
import org.yx.utils.FieldMap;

import com.xiaoleilu.hutool.util.StrUtil;

/**
 * 新增，修改或删除仓库信息
 * 
 * @author zxw
 * @since 2019/04/15
 **/
public class SbasSaveWarehouseDefinitionService extends BaseService {
	public SbasSaveWarehouseDefinitionOut saveWarehouseDefinition(SbasSaveWarehouseDefinitionIn in) throws Exception {
		if (StrUtil.isBlank(in.getWhCode())) {
			throw new BizException("SYS-0004", FieldMap.of("parameter", "whCode"));
		}

		SbasStoDef stoDef = selectOneLocked(SbasStoDef.class, in.getUserOrgCode(), in.getWhCode());
		if (stoDef == null) {
			if (Character.valueOf('D').equals(in.getWhStatus())) {
				throw new BizException("SBAS-0006", "仓库信息 ${whCode} 数据不存在。", FieldMap.of("whCode", in.getWhCode()));
			}

			stoDef = new SbasStoDef();
			stoDef.setOrgCode(in.getUserOrgCode());
			stoDef.setWhCode(in.getWhCode());
			stoDef.setWhName(in.getWhName());
			stoDef.setWhType(in.getWhType());
			stoDef.setWhStatus('N');

			insert(stoDef);
		} else {
			if (Character.valueOf('D').equals(in.getWhStatus())) {
				stoDef.setWhStatus('D');

				update(stoDef, "whStatus");
			} else {
				copyProperties(in, stoDef);

				update(stoDef);
			}
		}

		SbasSaveWarehouseDefinitionOut out = new SbasSaveWarehouseDefinitionOut();

		return out;
	}
}